"use strict";
const { Model } = require("sequelize");
const { IdGenerator } = require("../utils/idGenerator");
module.exports = (sequelize, DataTypes) => {
  class Generator extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  }

  Generator.init(
    {
      id: {
        type: DataTypes.INTEGER.UNSIGNED, // 无符号整数类型
        allowNull: false,
        primaryKey: true,
        defaultValue: function () {
          return IdGenerator.getNextID(); // 使用封装的ID生成方法
        },
      },

      tableName: DataTypes.STRING, // 数据库表名

      modelName: DataTypes.STRING, // 模型名

      fields: {
        type: DataTypes.TEXT,
        set: function (value) {
          this.setDataValue("fields", JSON.stringify(value));
        },
        get: function () {
          return JSON.parse(this.getDataValue("fields"));
        },
      }, // 字段名

      title: DataTypes.STRING, // 业务名

      routerPath: DataTypes.STRING, // 模块名

      createdAt: {
        allowNull: false,
        type: DataTypes.DATE,
      },
      updatedAt: {
        allowNull: false,
        type: DataTypes.DATE,
      },
    },
    {
      sequelize,
      modelName: "Generator",
    },
  );
  return Generator;
};
